﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Service.Data;
using System.Data.Common;
using Sinacor.Infra.Service.Common;
using System.Data;

namespace Sinacor.OMM.Deployment.Configurator.ResourceAccess
{
    public class HFTMessageManager : BaseResourceAccess
    {
        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="companyId"></param>
        public HFTMessageManager(Int32 companyId)
            : base(companyId)
        {

        }

        #endregion

        #region Methods

        public List<Entity.HFTMessageManager> ConsultarHFT()
        {
            //
            String sqlCommand = String.Empty;
            List<Entity.HFTMessageManager> retorno = null;

            sqlCommand = @"SELECT a.COD_PROS_IAFR
                                  ,a.NUM_MAC_ETRB_PROS_IAFR
                                  ,a.COD_REPRIO
                                  ,a.NOME_DRTO_ARQ_CTRL
                                  ,(SELECT TXT_CNTD_PRDD FROM TBOREPRIO_DET WHERE NOME_PRDD = 'CONNECTION_STRING' AND COD_REPRIO = a.COD_REPRIO) DESC_REPRIO
                                  ,(SELECT TXT_CNTD_PRDD FROM TBOREPRIO_DET WHERE NOME_PRDD = 'QUEUE_MESSAGE_LIMIT' AND COD_REPRIO = a.COD_REPRIO) MSG_LIMIT_REPRIO
                                  ,(SELECT TXT_CNTD_PRDD FROM TBOREPRIO_DET WHERE NOME_PRDD = 'QUEUE_BYTES_LIMIT' AND COD_REPRIO = a.COD_REPRIO) BYTES_LIMIT_REPRIO
                           FROM TBOPROS_IAFR a";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                retorno = this.FindAll<Entity.HFTMessageManager>(command,
                    reader =>
                    {
                        Entity.HFTMessageManager item = new Entity.HFTMessageManager();

                        item.CodigoHFTMessageManager = reader.GetValue<int>("COD_PROS_IAFR");
                        item.MacAddress = reader.GetValue<String>("NUM_MAC_ETRB_PROS_IAFR");
                        item.DiretorioArquivo = reader.GetValue<String>("NOME_DRTO_ARQ_CTRL");
                        item.Repositorio.Codigo = reader.GetValue<int>("COD_REPRIO");
                        item.Repositorio.Fila = reader.GetValue<String>("DESC_REPRIO");
                        item.Repositorio.LimiteMensagem = reader.GetValue<int>("MSG_LIMIT_REPRIO");
                        item.Repositorio.LimiteTamanho = reader.GetValue<int>("BYTES_LIMIT_REPRIO");

                        item.HFTRules = ConsultarHFTRules(item);

                        return item;
                    });
            }

            return retorno;
        }

        public List<Entity.HFTMessageRule> ConsultarHFTRules(Entity.HFTMessageManager entity)
        {
            //
            String sqlCommand = String.Empty;
            List<Entity.HFTMessageRule> retorno = null;

            sqlCommand = @"SELECT a.COD_CNFG_DIST_MSG_IAFR
                                  ,a.CD_CLIENTE
                                  ,a.NUM_PRTA_INIC
                                  ,a.NUM_PRTA_FIM
                                  ,a.COD_PROS_IAFR
                           FROM TBOCNFG_DIST_MSG_IAFR a
                           WHERE COD_PROS_IAFR = :COD_PROS_IAFR";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "COD_PROS_IAFR", DbType.Int32, entity.CodigoHFTMessageManager);

                retorno = this.FindAll<Entity.HFTMessageRule>(command,
                    reader =>
                    {
                        Entity.HFTMessageRule item = new Entity.HFTMessageRule();

                        item.CodigoHFTRule = reader.GetValue<int>("COD_CNFG_DIST_MSG_IAFR");
                        item.CodigoCliente = reader.GetValue<int?>("CD_CLIENTE");
                        item.PortaInicial = reader.GetValue<int?>("NUM_PRTA_INIC");
                        item.PortaFinal = reader.GetValue<int?>("NUM_PRTA_FIM");
                        item.CodigoHFTMessageManager = reader.GetValue<int>("COD_PROS_IAFR");

                        return item;
                    });
            }

            return retorno;
        }

        public void ExcluirHFT(Entity.HFTMessageManager entity)
        {
            foreach (Entity.HFTMessageRule rule in entity.HFTRules)
                ExcluirHFTRule(rule);

            String sqlCommand = String.Empty;

            sqlCommand = @"DELETE FROM TBOPROS_IAFR a
                           WHERE a.COD_PROS_IAFR = :COD_PROS_IAFR";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "COD_PROS_IAFR", DbType.Int32, entity.CodigoHFTMessageManager);
                
                this.DataBase.ExecuteNonQuery(command);
            }
        }

        private void ExcluirHFTRule(Entity.HFTMessageRule entity)
        {
            String sqlCommand = String.Empty;

            sqlCommand = @"DELETE FROM TBOCNFG_DIST_MSG_IAFR a
                           WHERE a.COD_CNFG_DIST_MSG_IAFR = :COD_CNFG_DIST_MSG_IAFR";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "COD_CNFG_DIST_MSG_IAFR", DbType.Int32, entity.CodigoHFTRule);

                this.DataBase.ExecuteNonQuery(command);
            }
        }

        public int IncluirHFT(Entity.HFTMessageManager entity)
        {
            int retorno = -1;

            String sql = String.Empty;
            String sqlCommand = String.Empty;

            sql = @"SELECT max(NVL(COD_PROS_IAFR,0)) FROM TBOPROS_IAFR";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql))
            {
                retorno = Convert.ToInt32("0" + this.DataBase.ExecuteScalar(command)) + 1;
            }
            
            sqlCommand = @"INSERT INTO TBOPROS_IAFR a
                           (a.COD_PROS_IAFR,a.NUM_MAC_ETRB_PROS_IAFR ,a.COD_REPRIO ,a.NOME_DRTO_ARQ_CTRL)
                            VALUES
                            (:COD_PROS_IAFR, 
                            :NUM_MAC_ETRB_PROS_IAFR,
                            :COD_REPRIO,
                            :NOME_DRTO_ARQ_CTRL)";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "COD_PROS_IAFR", DbType.Int32, retorno);
                this.DataBase.AddInParameter(command, "NUM_MAC_ETRB_PROS_IAFR", DbType.String, entity.MacAddress);
                this.DataBase.AddInParameter(command, "COD_REPRIO", DbType.Int32, entity.Repositorio.Codigo);
                this.DataBase.AddInParameter(command, "NOME_DRTO_ARQ_CTRL", DbType.String, entity.DiretorioArquivo);

                this.DataBase.ExecuteNonQuery(command);
            }

            foreach (Entity.HFTMessageRule rule in entity.HFTRules)
            {
                rule.CodigoHFTMessageManager = retorno;
                IncluirHFTRule(rule);
            }

            return retorno;
        }

        public int IncluirHFTRule(Entity.HFTMessageRule entity)
        {
            int retorno = -1;

            String sql = String.Empty;
            String sqlCommand = String.Empty;

            sql = @"SELECT max(NVL(COD_CNFG_DIST_MSG_IAFR,0)) FROM TBOCNFG_DIST_MSG_IAFR";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql))
            {
                retorno = Convert.ToInt32("0" + this.DataBase.ExecuteScalar(command)) + 1;
            }

            sqlCommand = @"INSERT INTO TBOCNFG_DIST_MSG_IAFR a
                           (a.COD_CNFG_DIST_MSG_IAFR, a.CD_CLIENTE, a.NUM_PRTA_INIC, a.NUM_PRTA_FIM, a.COD_PROS_IAFR)
                            VALUES
                            (:COD_CNFG_DIST_MSG_IAFR, 
                            :CD_CLIENTE,
                            :NUM_PRTA_INIC,
                            :NUM_PRTA_FIM,
                            :COD_PROS_IAFR)";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "COD_CNFG_DIST_MSG_IAFR", DbType.Int32, retorno);
                this.DataBase.AddInParameter(command, "CD_CLIENTE", DbType.Int32, entity.CodigoCliente);
                this.DataBase.AddInParameter(command, "NUM_PRTA_INIC", DbType.Int32, entity.PortaInicial);
                this.DataBase.AddInParameter(command, "NUM_PRTA_FIM", DbType.Int32, entity.PortaFinal);
                this.DataBase.AddInParameter(command, "COD_PROS_IAFR", DbType.Int32, entity.CodigoHFTMessageManager);

                this.DataBase.ExecuteNonQuery(command);
            }

            return retorno;
        }

        #endregion
    }
}